package cn.edu.djtu.fw.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.edu.djtu.fw.dao.CarMapper;
import cn.edu.djtu.fw.model.Car;
import cn.edu.djtu.fw.service.CarServiceI;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

@Service("carService")
public class CarServiceImpl implements CarServiceI {
	private CarMapper carMapper;

	public CarMapper getCarMapper() {
		return carMapper;
	}

	@Autowired
	public void setCarMapper(CarMapper carMapper) {
		this.carMapper = carMapper;
	}

	@Override
	public boolean delete(int nid) {
		try {
			carMapper.deleteByPrimaryKey(nid);
			return true;
		} catch (Exception e) {
		}
		return false;
	}

	@Override
	public boolean update(Car car) {
		try {
			carMapper.updateByPrimaryKey(car);
			return true;
		} catch (Exception e) {
		}
		return false;
	}

	@Override
	public boolean insert(Car car) {
		try {
			carMapper.insert(car);
			return true;
		} catch (Exception e) {
		}
		return false;
	}

	@Override
	public Car getCarById(int nid) {
		return carMapper.selectByPrimaryKey(nid);
	}

	@Override
	public Map<String, Object> getAllCar(int page, int rows, Car car) {
		try {
			PageHelper.startPage(page, rows);
			List<Car> cars = carMapper.getAllCar(car);
			Map<String, Object> carMap = new HashMap<String, Object>();
			carMap.put("rows", cars);
			carMap.put("total", ((Page<Car>) cars).getTotal());
			return carMap;
		} catch (Exception e) {
		}
		return null;
	}
}
